package ru.reinvent.perspectiva.fragments;

import com.actionbarsherlock.app.SherlockFragment;

import ru.reinvent.perspectiva.R;
import ru.reinvent.perspectiva.cash.ContactsModel;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ContactsFragment extends SherlockFragment 
	{
	
	protected ListView contactsList;
	
	public ContactsFragment() {;}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
		{
		if (contactsList != null)
			{
			if (contactsList.getParent() != null) ((ViewGroup) contactsList.getParent()).removeView(contactsList);
			return contactsList;
			}
		
		contactsList = new ListView(getActivity());
		contactsList.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		contactsList.setCacheColorHint(Color.TRANSPARENT);
		contactsList.setSelector(new StateListDrawable());
		
		ContactsAdapter adapter = new ContactsAdapter(getActivity());
		ContactsModel contacts = new ContactsModel(getActivity());
		adapter.add(contacts.getMoscowContact());
		adapter.add(contacts.getPeterContact());
		adapter.add(contacts.getEkaterinburgContact());
		adapter.add(contacts.getAlmatiContact());
		adapter.add(contacts.getKyevContact());

		contactsList.setAdapter(adapter);
		return contactsList;		
		}

	private class ContactsAdapter extends ArrayAdapter<ContactsModel.Contact> 
		{
		private final Activity activity;
	
		public ContactsAdapter(Activity activity) 
			{
			super(activity, R.layout.contacts_item);
			this.activity = activity;
			}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
			{
			View rowView = convertView;
			
			if (rowView == null) 
				{
				LayoutInflater inflater = activity.getLayoutInflater();
				rowView = inflater.inflate(R.layout.contacts_item, parent, false);
			
				ViewHolder viewHolder = new ViewHolder();
				viewHolder.town = (TextView) rowView.findViewById(R.id.contacts_item_town);
				viewHolder.address = (TextView) rowView.findViewById(R.id.contacts_item_address);
				viewHolder.hours = (TextView) rowView.findViewById(R.id.contacts_item_hours);
				viewHolder.phones[0] = (TextView) rowView.findViewById(R.id.contacts_item_phone1);
				viewHolder.phones[1] = (TextView) rowView.findViewById(R.id.contacts_item_phone2);
				viewHolder.phones[2] = (TextView) rowView.findViewById(R.id.contacts_item_phone3);
				viewHolder.phones[3] = (TextView) rowView.findViewById(R.id.contacts_item_phone4);
				viewHolder.phoneBlocks[0] = rowView.findViewById(R.id.contacts_item_phone_group1);
				viewHolder.phoneBlocks[1] = rowView.findViewById(R.id.contacts_item_phone_group2);
				viewHolder.phoneBlocks[2] = rowView.findViewById(R.id.contacts_item_phone_group3);
				viewHolder.phoneBlocks[3] = rowView.findViewById(R.id.contacts_item_phone_group4);
	
				OnClickListener call = new OnClickListener() 
					{
					@Override
					public void onClick(View v) 
						{
						TextView phone = (TextView) v;
						
						Intent intent = new Intent(Intent.ACTION_CALL);
						intent.setData(Uri.parse("tel:" + phone.getText().toString()));
						activity.startActivity(intent);
						}
					};
				for (TextView phoneView : viewHolder.phones) phoneView.setOnClickListener(call);	
	
				rowView.setTag(viewHolder);
				}
	
			ViewHolder holder = (ViewHolder) rowView.getTag();
			ContactsModel.Contact contact = getItem(position);
			
			if (contact.getTown().length() > 0) 
				{
				holder.town.setVisibility(View.VISIBLE);
				holder.town.setText(contact.getTown());
				}
			else holder.town.setVisibility(View.GONE);
			
			if (contact.getAddress().length() > 0) 
				{
				holder.address.setText(contact.getAddress());
				holder.address.setVisibility(View.VISIBLE);
				}
			else holder.address.setVisibility(View.GONE);
			
			if (contact.getHours().length() > 0)
				{
				holder.hours.setText(contact.getHours());
				holder.hours.setVisibility(View.VISIBLE);
				}
			else holder.hours.setVisibility(View.GONE);
			
			for (int i=0; i<contact.getPhones().size(); i++) 
				{
				holder.phoneBlocks[i].setVisibility(View.VISIBLE);
				holder.phones[i].setText(contact.getPhone(i));
				}
			for (int i=contact.getPhones().size(); i<4; i++) holder.phoneBlocks[i].setVisibility(View.GONE);
			
			return rowView;
			}
	
		private class ViewHolder 
			{
			public TextView town;
			public TextView address;
			public TextView hours;
			public TextView[] phones = new TextView[4];
			public View[] phoneBlocks = new View[4];
			}
		}
	}
